package org.september.pisces.user.permission.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.september.smartdao.anno.AutoIncrease;
import org.september.smartdao.anno.Entity;
import org.september.smartdao.anno.Id;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;

import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import com.gitee.sunchenbin.mybatis.actable.annotation.DefaultValue;
import com.gitee.sunchenbin.mybatis.actable.annotation.IsAutoIncrement;
import com.gitee.sunchenbin.mybatis.actable.annotation.IsKey;
import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant;

import jakarta.validation.constraints.Size;
import lombok.Data;


@Data
@Entity
@Table("pisces_system_user")
public class SystemUser {


    private static transient final long serialVersionUID = -6743412801618993576L;

    /**
     *
     */
    @Id
    @AutoIncrease
    @IsKey
    @IsAutoIncrement
    @Column(name = "id", length = 20)
    private Long id;

    /**
     *
     */
    @Size(max = 64)
    @Column(name = "username", length = 64, isNull = false)
    private String username;


    /**
     * 真实姓名
     */
    @Size(max = 64)
    @Column(name = "realname", length = 64, comment = "真实姓名")
    private String realname;

    @Size(max = 128)
    @Column(name = "password", length = 128, isNull = false)
    private String password;

    @Column(name = "role_ids", comment = "角色id，英文分号分隔")
    private String roleIds;
    
    private String roleNamesString;


    /**
     *
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "last_login_time", type = MySqlTypeConstant.DATETIME)
    private Date lastLoginTime;

    @Size(max = 32)
    @Column(name = "phone")
    private String phone;

    @Size(max = 32)
    @Column(name = "session_id", length = 128)
    private String sessionId;

    @Column(name = "email")
    private String email;

    @Column(name = "avatar",type=MySqlTypeConstant.LONGTEXT)

    private String avatar;

    @Column(name = "station_ids")
    private String stationIds;
    
    @Column(name = "dept_id", comment = "所属部门Id")
    private Long deptId;
    
    @Column(name = "full_dept_id", comment = "所属部门全路径Id")
    private String fullDeptId;
    
    /**
    *
    */
   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
   @Column(name = "addtime", type = MySqlTypeConstant.DATETIME)
   protected Date addtime;

   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
   @Column(name = "update_time", type = MySqlTypeConstant.DATETIME)
   protected Date updateTime;

   /**
    *
    */
   @Column(name = "adduid", length = 20)
   protected Long adduid;

   /**
    *
    */
   @Column(name = "delete_flag", type = MySqlTypeConstant.SMALLINT, length = 6, isNull = false)
   @DefaultValue("0")
   protected Integer deleteFlag;

   /**
    *
    */
   @Column(name = "delete_uid", length = 20)
   protected Long deleteUid;

   /**
    *
    */
   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
   @Column(name = "delete_time", type = MySqlTypeConstant.DATETIME)
   protected Date deleteTime;

    private transient boolean sessionLogin = false;
    private transient boolean ssoLogin = false;
    
    private transient String webOnline = "离线";
    private transient String appOnline = "离线";
    //used for TokenSession
    private transient String token;
    
    private transient String deptName;
    
    private transient List<String> permitUrlList = new ArrayList<>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public boolean hasPermission(String url) {
    	if(!StringUtils.hasText(url)) {
    		return false;
    	}
    	return permitUrlList.contains(url);
    }

}
